//
//  StudentSelectionViewController.m
//  iOsiris
//
//  Created by bram on 1/11/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "StudentSelectionViewController.h"

#import "SelectionViewController.h";
#import "PeriodDetailsViewController.h";
#import "RegisterViewController.h";
#import "RegisterReportViewController.h";
#import "Selection.h";
#import "Database.h";

@implementation StudentSelectionViewController

@synthesize user;
@synthesize periodField;

@synthesize picker;
@synthesize yearPeriods;
@synthesize years;
@synthesize periods;

- (void) setUserAccount:(id)userAccount
{
	user = userAccount;
	periodField.text = user.username;
}

- (NSInteger) getYearRow
{
	Database *database = [[Database alloc] init];
	
	NSMutableArray *yearsArray = [[NSMutableArray alloc] init];
	yearsArray = [database getYears];
	
	NSInteger yearRow = [picker selectedRowInComponent:kYearComponent];
	IdValue *year1 = [yearsArray objectAtIndex:yearRow];
	
	return year1.id;
}

- (NSInteger) getPeriodRow
{
	NSInteger periodRow = [picker selectedRowInComponent:kPeriodComponent];
	IdValue *period = [periods objectAtIndex:periodRow];
	
	return period.id;
}

- (IBAction) btnLogout:(id)sender
{
	[self.view removeFromSuperview];
}

- (IBAction) btnDetailsView:(id) sender 
{
	PeriodDetailsViewController *detailsViewController = [[PeriodDetailsViewController alloc] initWithNibName:@"DetailsView" bundle:nil];
	
	NSInteger year = [self getYearRow];
	NSInteger period = [self getPeriodRow];
	
	Selection *selection = [[[Selection alloc] init] autorelease];
		
	// get de DBAccess Object
	Database *database = [[Database alloc] init];
		
	//get the selection array from the database
	selection = [database getSelectedPeriodForYear:year :period];
		
	//close the database
	[database closeDatabase];
		
	//release the database to free memory
	[database release];
				
	//set the labels for selected period
	[detailsViewController setLabelsForPeriod:selection];
		
	[self.view addSubview:detailsViewController.view];
}

- (IBAction) btnRegister:(id) sender
{
	Database *database = [[Database alloc] init];
	
	NSInteger year = [self getYearRow];
	NSInteger period = [self getPeriodRow];
	
	RegisterViewController *registerView = [[RegisterViewController alloc] initWithNibName:@"RegisterView" bundle:nil];
	[registerView setUserAccount:user];
		
	int count = [database getOpenRegisterCount:period: 1: user.userid];
	
	if (count > 0) {
		[registerView setYearAndPeriod:year :period];
	}
	else {
		[registerView setNoCourseOpen:@"There are no courses open for register!"];
	}
	
	//close the database
	[database closeDatabase];
	
	//release the database to free memory
	[database release];
	
	[self.view addSubview:registerView.view];
}

-(IBAction) btnRegisterReport:(id) sender
{
	Database *database = [[Database alloc] init];
	
	NSInteger period = [self getPeriodRow];
	
	RegisterReportViewController *registerReportView = [[RegisterReportViewController alloc] initWithNibName:@"RegisterReportView" bundle:nil];
	[registerReportView setUserAccount:user];
	int count = [database getRegisteredCoursesCount:period: 1: user.userid];
	
	if (count > 0) {
		[registerReportView getRegisteredCourses];
	}
	else {
		[registerReportView setNoRegisterdCourses:@"There are none registerd courses!"];
	}
	
	//close the database
	[database closeDatabase];
	
	//release the database to free memory
	[database release];
	
	
	
	[self.view addSubview:registerReportView.view];
}

/*
// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 
{
	Database *database = [[Database alloc] init];
	
	NSMutableArray *yearsArray = [[NSMutableArray alloc] init];
	NSMutableArray *periodsArray = [[NSMutableArray alloc] init];
	
	yearsArray = [database getYears];
	periodsArray = [database getPeriods];
	
	years = [[NSArray alloc] initWithArray:yearsArray];
	//periods = [[NSArray alloc] initWithArray:periodsArray];
	
	
	NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
	
	int i;
	for (i = 0; i < 4; i++) {
		IdValue *year;
		year = [yearsArray objectAtIndex:i];
		NSMutableArray *ar = [[NSMutableArray alloc] init];
		for (int j = 0; j < [periodsArray count]; j++) {
			IdValue *period;
			period = [periodsArray objectAtIndex:j];
			if (year.id == period.yearid) {
				[ar addObject:period];
				
				[period release];
			}
			
		}
		[dictionary setObject:ar forKey:year.value];
		
		NSLog(@"dictionary: %@", dictionary);
		[year release];
		NSLog(@"dictionary: %@", dictionary);

	}
	
	NSLog(@"dictionary: %@", dictionary);
	
	self.yearPeriods = dictionary;
	[dictionary release];
	
	NSArray *components = [self.yearPeriods allKeys];
	NSArray *sorted = [components sortedArrayUsingSelector:@selector(compare:)];
	NSLog(@"components: %@", components);
	self.years = sorted;
	
	IdValue *year = [self.years objectAtIndex:0];
	NSArray *array = [yearPeriods objectForKey:year];
	self.periods = array;
		
	//[yearsArray release];
	//[periodsArray release];	
    [super viewDidLoad];	
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[picker release];
	[yearPeriods release];
	[years release];
	[periods release];
    [super dealloc];
}

#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
	return 2;	
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
	if(component == kYearComponent)
		return [self.years count];
	return [self.periods count];
}

#pragma mark Picker delegate Methods

// overide the standard text layout for the rows in the picker
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
	UILabel *returnYearValue = (id) view;
	UILabel *returnPeriodValue = (id) view;
	
	if (!returnYearValue || !returnPeriodValue) {
		returnYearValue = [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)] autorelease];
		returnPeriodValue = [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)] autorelease];
	}
	returnYearValue.font = [UIFont fontWithName:@"Verdana" size:14];
	returnYearValue.textColor = [UIColor blueColor];
	
	returnPeriodValue.font = [UIFont fontWithName:@"Verdana" size:14];
	returnPeriodValue.textColor = [UIColor blueColor];
	
	IdValue *year;
	if(component == kYearComponent)
	{
		year = [years objectAtIndex:row];
		returnYearValue.text =  [self.years objectAtIndex:row];
		return returnYearValue;
	}
	if(component == kPeriodComponent)
	{
		IdValue *period = [periods objectAtIndex:row];		
		returnPeriodValue.text = period.periodName;
	
		return returnPeriodValue;
	}
	return nil;
}

/*
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
	IdValue *year;
	if(component == kYearComponent)
	{
		year = [years objectAtIndex:row];
		return [self.years objectAtIndex:row];
	}
	IdValue *period = [periods objectAtIndex:row];		
	return period.periodName;
}
*/
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
	IdValue *year;
	if(component == kYearComponent)
	{	
		year = [years objectAtIndex:row];
		
		NSArray *array = [yearPeriods objectForKey:year];
		self.periods = array;
		
		//returns periods picker scroll row to first item
		[picker selectRow:0 inComponent:kPeriodComponent animated:YES];
		[picker reloadComponent:kPeriodComponent];
	}	
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
	if(component == kPeriodComponent)
		return 240;
	return 55;
}
@end
